package com.cykj.pos.util;

import com.cykj.common.constant.Constants;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static Date asDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate asLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    public static String localeDateTime2String(LocalDateTime date,DateTimeFormatter formatter) {
        return date.format(formatter);
    }
    public static String localeDate2String(LocalDate date,DateTimeFormatter formatter) {
        return date.format(formatter);
    }
    public static String Date2String(Date date,String formatterString) {
        LocalDateTime localDateTime = DateUtils.asLocalDateTime(date);
        return localDateTime.format(DateTimeFormatter.ofPattern(formatterString));
    }
    public static LocalDate stringToLocalDate(String time,DateTimeFormatter formatter) {
        return LocalDate.parse(time, formatter);
    }
    public static LocalDateTime stringToLocalDateTime(String time,DateTimeFormatter formatter) {
        return LocalDateTime.parse(time, formatter);
    }
    public static String getCaculateYearAndMonth(String thisMonth,String formatter){
        LocalDate localDate = LocalDate.now();
        if("last".equals(thisMonth)){
            localDate = localDate.minusMonths(1);
        }
        if(formatter.contains("-")){
            return localDate.format(DateTimeFormatter.ofPattern(formatter)).substring(0,7);
        }
       return localDate.format(DateTimeFormatter.ofPattern(formatter)).substring(0,6);
    }
    // 当前天减几天
    public static String dateSubOne(Date date,int num){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();// new一个Calendar类，把Date放进去
        calendar.setTime(date);
        calendar.add(Calendar.DATE, num);// 此时的时间根据要减的天数 -2 是昨天
        return df.format(calendar.getTime());
    }
    // 当前月份减几月
    public static String dateSubMonth(Date date,int num){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();// new一个Calendar类，把Date放进去
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,num);
        return df.format(calendar.getTime());
    }
    // 当前天加上多少天
    public static Date addDays(Date d, int days)
    {
        d.setTime(d.getTime() + days * 1000 * 60 * 60 * 24);
        return d;
    }
    public static String cutOutDate(String date,int num){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat df = null;
        if (num==1){
             df = new SimpleDateFormat("MM/dd");
        }

        if (num==2){
             df = new SimpleDateFormat("yyyy/MM");
        }
        return df.format(time);
    }

    public static String cutOutMonth(String date,int num){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
        Date time = null;
        try {
            time = sf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat df = null;
        if (num==1){
            df = new SimpleDateFormat("MM/dd");
        }

        if (num==2){
            df = new SimpleDateFormat("yyyy/MM");
        }
        return df.format(time);
    }

    // 返回系统当前日期
    public static String dateFormatString(Date date){ // 默认返回 yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(date);
    }
    // 返回系统当前日期
    public static String dateFormatStringToo(Date date){ // 默认返回 yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(date);
    }
    public static String dateFormatString(Date date,String pattern){
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        return sf.format(date);
    }
    /**
     * 盛付通20210604151213转时间格式
     * @param logTime
     * @return
     * @throws ParseException
     */
    public static String getTime(String logTime) throws Exception {
        String time =logTime ;
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMddHHmmss");
        time = formatter1.format(formatter2.parse(time));
        return time;
    }
    public static String getReverseTime(String logTime) throws Exception {
        String time =logTime ;
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
        time = formatter1.format(formatter2.parse(time));
        return time;
    }


    public static String dateConvertion(String date1) {
        String longDate = "";
        try {
            //   yyyyMMdd转yyyy-MM-dd
            Date format1 = null;
            format1 = new SimpleDateFormat("yyyyMMdd").parse(date1);
            longDate = new SimpleDateFormat("yyyy-MM-dd").format(format1);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return longDate;
    }

    public static void main(String[] args){
        /*LocalDateTime localDateTime = LocalDateTime.now();
        String startTime = DateUtils.localeDateTime2String(localDateTime, Constants.DATETIME_FORMATTER);
        System.out.println(startTime);
        LocalDateTime endLocalTime = localDateTime.plusMinutes(5);
        String endTime = DateUtils.localeDateTime2String(endLocalTime, Constants.DATETIME_FORMATTER);
        System.out.println(endTime);*/
        /*String formatedDate = DateUtils.getCaculateYearAndMonth("this","yyyy-MM-dd");
        formatedDate = Date2String(new Date(),"yyyy-MM-dd HH:mm:ss");
        System.out.println(formatedDate);*/
        /*String ok = DateUtils.dateSubMonth(new Date(),0);
        System.out.println(ok);*/
        /*Date date = new Date();
        String yesterday = DateUtils.dateSubOne(date,-1);
        System.out.println(yesterday);*/
        try {
            System.out.println(dateSubMonth(new Date(),-1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
